﻿using System.ComponentModel;
using System.Windows.Forms;
using SourceGrid;
using SourceGrid.Cells.Controllers;
using ZeBobo5.LibraryHelper.Core.Grid.Controllers;

namespace ZeBobo5.LibraryHelper.Core.Grid.Controllers
{
    public class SortableHeaderController : SortableHeader
    {
        public SortableHeaderController()
        {
        }

        public SortableHeaderController(IRangeLoader p_RangeToSort, IRangeLoader p_HeaderRange)
            : base(p_RangeToSort, p_HeaderRange)
        {
        }

        public override void OnMouseDown(CellContext sender, MouseEventArgs e)
        {
            base.OnMouseDown(sender, e);
            if (sender.Grid.Selection.ActivePosition != Position.Empty)
            {
                if (
                    sender.Grid.GetCell(sender.Grid.Selection.ActivePosition).FindController(
                        typeof (RowFocusChangingController)) != null)
                {
                    sender.Grid.GetCell(sender.Grid.Selection.ActivePosition).FindController(
                        typeof (RowFocusChangingController)).OnFocusLeaving(
                        new CellContext(sender.Grid, sender.Grid.Selection.ActivePosition), new CancelEventArgs());
                }
            }
        }

        public override void OnMouseUp(CellContext sender, MouseEventArgs e)
        {
            base.OnMouseUp(sender, e);
            if (sender.Grid.Selection.ActivePosition != Position.Empty)
            {
                if (
                    sender.Grid.GetCell(sender.Grid.Selection.ActivePosition).FindController(
                        typeof (RowFocusChangingController)) != null)
                {
                    sender.Grid.GetCell(sender.Grid.Selection.ActivePosition).FindController(
                        typeof (RowFocusChangingController)).OnFocusEntering(
                        new CellContext(sender.Grid, sender.Grid.Selection.ActivePosition), new CancelEventArgs());
                }
            }
        }
    }
}